package test033;

import org.apache.ibatis.datasource.pooled.PooledDataSource;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;


import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;

/**
 * Hello world!
 *
 */
public class App {

    public static void main( String[] args ) {

        SqlSessionFactory factory = null;
        try {
            //InputStream inputStream = Resources.getResourceAsStream("config.xml");
            SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
            //方法1：采用xml配置文件生成  【店长推荐】
            //factory = builder.build(inputStream);

            //方法2：采用java API的方式 【不推荐】
            String driver = "com.mysql.cj.jdbc.Driver";
            String url = "jdbc:mysql://localhost:3306/test01?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true&allowPublicKeyRetrieval=true";
            String username = "root";
            String password = "123456";
            DataSource dataSource = new PooledDataSource(driver,url,username,password);
            TransactionFactory transactionFactory = new JdbcTransactionFactory();
            Environment environment = new Environment("mycom",transactionFactory,dataSource);
            Configuration configuration = new Configuration(environment);
            configuration.addMapper(StudentDao.class);
            factory = builder.build(configuration);




            //System.out.println("1.配置文件图纸："+inputStream );
            System.out.println("2.建造工厂"+factory);
        } catch (Exception e) {
            e.printStackTrace();
        }


        SqlSession sqlSession = factory.openSession();
        System.out.println("3.session："+sqlSession);
        //System.out.println("4.居然没有写实现类，也能获得实现类的实例："+studentDao);

        //方案1：
        StudentDao studentDao = sqlSession.getMapper(StudentDao.class);
        Student student = studentDao.findStudentById(1);
        System.out.println(student);//

        Student student2 = new Student();
        student2.setStudId(88);
        student2.setName("蔡小姐");
        student2.setEmail("caixukun@163.com");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        java.util.Date date2 = null;
        try {
            date2 = simpleDateFormat.parse("2007-08-08");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        student2.setDob( new java.sql.Date(  date2.getTime()  ) );

        studentDao.insertStudent(  student2  );
        //int lastId = studentDao.getLastId();
        //student2.setStudId( lastId );
        sqlSession.commit();
        System.out.println("ID:"+student2.getStudId());

        sqlSession.close();


        //.....................
        //方案2：
        //Student student2 = sqlSession.selectOne("test033.StudentDao.findStudentById", 1);




    }
}
